
var mid = $.cookie('mid');
var token = $.cookie('token');

if (!(mid && token)) {
    var he=window.location.href;
    if(he.indexOf('mid')!=-1){
        var m=he.split("=")[1].split("&")[0];
        var t=he.split("=")[2].split("&")[0];
        $.cookie('mid',m,{expires:1});
        $.cookie('token',t,{expires:1});
        window.location.href='http://localhost:63342/hospital_web/'+'html/index.html'
    } else {
        window.location.href =siteUrl;
    }
}



var mydate=new Date();
var month=mydate.getMonth()+1;
var date=mydate.getDate();
var week=mydate.getDay();
var arr=['周日','周一','周二','周三','周四','周五','周六','周日','周一','周二','周三','周四','周五','周六'];
for(var i=0;i<7;i++){
    $('.date_list li').eq(i).find('.week').html(arr[week+i]);
    $('.date_list li').eq(i).find('.month_date').html(month+'-'+(date+i));
    $('.date1 li').eq(i).find('.week').html(arr[week+i]);
    $('.date1 li').eq(i).find('.month_date').html(month+'-'+(date+i))
}
$('.date_list li').find('.week').eq(0).html('今天');
$('.date1 li').find('.week').eq(0).html('今天');
$('.date1 li').find('.month_date').eq(0).html(month+'-'+date);
//下拉
$('#header p').on('touchstart',function () {
    $('.xiala').toggleClass('xiala1')
    $('.keshi_list').toggle();
})
//tab日期
$('.date_active').on('touchstart', function () {
    $(this).siblings().find('div').removeClass('tag')
    $(this).find('div').addClass('tag')
});

var next='';
var Dt=0;
var data_back=false;
    $(window).scroll(function () {
        var windowheight=$(window).height();//窗口高度
        var scrolltop=$('body').scrollTop();//滚动条高度
        var eh=$(document).height();//文本高度
        if(eh==windowheight+scrolltop){
            $('.bot_loading').show();
            if(data_back){
                return false
            }
            data_back=true
            if(next==null)
            { console.log(next)
                $('.bot_loading').html('没有更多啦！');
                setInterval(function () {
                    $('.bot_loading').fadeOut(800)
                },2000)
                data_back=false
            }
            else
            { console.log(next)
                getData({url:next,data:{mid:mid,token:token,key:Dt}}, function (data) {
                    var html;
                    if(Dt>0)
                    {
                         html=template('other_doctor',data)
                    }
                    else
                    {
                        html=template('today_doctor',data)
                    }

                    $('.list').append(html);
                    $('.bot_loading').hide();
                    next=data.doctorlist.data.next_page_url;
                    data_back=false
                    abc();

                })
            }
        }else
        {
            $('.bot_loading').hide();
        }
    })
//请求科室
localStorage.removeItem('doc_id');
localStorage.removeItem('date');
getData({url:keshi,cache:true}, function (data) {
    var html=template('keshi',data);
    $('.dl').html(html);
    $('.dl dd').eq(0).addClass('active');
    $('.keshi_list dd').on('click',function () {
        $(this).addClass('active').siblings().removeClass('active')
        $('.keshi_list').hide();
        $('.xiala').removeClass('xiala1')
        var section_id=$(this).index()+1;
        localStorage.keshi_id=section_id;//localStorage科室ID
        $('.load').show();
        var da= $('#main .tag').parent().index();
        getData({url:index,data:{mid:mid,token:token,section_id:section_id,key:da}}, function (data) {
            $('.load').hide()
            if(da>0){
                var html=template('other_doctor',data);
            }else{
                var html=template('today_doctor',data);
            }

            $('.list').html(html);
            abc()
        })
    })
})
//点击首页预约按钮
function abc() {
    $('.yuyue').on('touchstart', function (event) {
        event.stopPropagation();
        $('#all').show();
        $('body').css('position','fixed');
        var id=$(this).attr('doctor_id');
        localStorage.doc_id=id;//localStorage医生ID
        getData({url:yuyue,data:{mid:mid,token:token,id:id}}, function (data) {
            var html=template('yuyue',data);
            $('.box_top').html(html);
            var htmlone=template('choose_date',data);
            $('.choose').html(htmlone);
            //点击日期请求
            $('.date1_list li').on('touchstart', function () {

                $('.choose').html('')
                var key1= $(this).index()
                localStorage.date=key1;////localStorage日期
                getData({url:yuyue,data:{mid:mid,token:token,id:id,key:key1}}, function (data) {
                    var htmlone=template('choose_date',data);
                    $('.choose').html(htmlone);
                    //点击弹窗预约
                    $('.yy').on('touchstart', function () {
                        var key2= $('#all .tag').parent().index();
                        localStorage.work_time=$(this).attr('work_time')//localStorage预约时间
                        var time=$(this).attr('work_time')
                        window.location.href='finish-reference.html?d_id='+id+'&time='+time+'&key='+key2;
                    })
                })

            })
            $('.yy').on('touchstart', function () {
                var key2= $('#all .tag').parent().index();
                localStorage.work_time=$(this).attr('work_time')//localStorage预约时间
                var time=$(this).attr('work_time')
                window.location.href='finish-reference.html?d_id='+id+'&time='+time+'&key='+key2;
            })
            var myScroll=new iScroll('wrapper',{
                vScroll:false,
                hScroll:true
            })
        })

    });

}
//请求当天医生列表
getData({url:index,data:{mid:mid,token:token}}, function (data) {
    $('.load').hide()
    $('.bot_loading').html('加载中...')
    var html=template('today_doctor',data);
    $('.yiyuyue').on('touchstart', function (e) {
        e.stopPropagation();
        $('.one a').css('pointer-events','none');
        setTimeout(function () {
            $('.one a').css('pointer-events','auto');
        },600)
    })
    $('.list').html(html);
   next=data.doctorlist.data.next_page_url;
    $('.yiyuyue').on('touchstart', function (e) {
        e.stopPropagation();
        $('.one a').css('pointer-events','none');
        setTimeout(function () {
            $('.one a').css('pointer-events','auto');
        },600)
    })
    $('.one').on('touchstart', function () {

        var d_id=$(this).find('.yuyue').attr('doctor_id')

        getData({url:doctor_info,data:{mid:mid,token:token,id:d_id}}, function () {

        })
    });
    $('.close').on('touchstart', function () {
        $('#all').hide();
        $('body').css('position','');
        $('.one').css('pointer-events','none');
        setTimeout(function () {
            $('.one').css('pointer-events','auto');
        },400)
    })
});
//点击日期请求医生列表
$('.date_list li').on('touchstart', function () {
    var today=$(this).index()
    if(today==0){
        window.location.href='http://localhost:63342/hospital_web/'+'html/index.html'
    }
    $('.bot_loading').html('加载中...')
    $('.list').hide();
    $('.list').html('');
    var key=$(this).index();
    var section_id=$('.active').index()+1;//科室ID
    $('.load').show()
    getData({url:index,data:{mid:mid,token:token,key:key,section_id:section_id}}, function (data) {
        $('.load').hide()
        $('.list').show();
        var html=template('other_doctor',data);
        $('.list').html(html);
        //点击预约
        next=data.doctorlist.data.next_page_url;
        Dt=key;
        console.log(next)

        abc();
        //跳转医师简介页面
        $('.one').on('touchstart', function () {
            var d_id=$(this).find('.yuyue').attr('doctor_id')
            getData({url:doctor_info,data:{mid:mid,token:token,id:d_id}}, function (data) {
            })
        })
        $('.close').on('touchstart', function () {
            $('#all').hide();
            $('body').css('position','');
            $('.one').css('pointer-events','none');
            setTimeout(function () {
                $('.one').css('pointer-events','auto');
            },400)
        })
    })
})

